package server

import (
	"fmt"
	"html"
	"regexp"

	"listExcel/util"
	"os"

	"strconv"

	"time"

	"github.com/360EntSecGroup-Skylar/excelize/v2"

	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
)

//去除表格特殊符号
func ClearExcel(excel_file string) {
	ROOT_PATH = os.Getenv("ROOT_PATH")
	subClass := os.Getenv("subClass")

	fmt.Println(subClass)

	sheet := os.Getenv("Sheet")

	_, rows, f, maxRow := readExcel(excel_file, sheet, subClass)
	fmt.Println("maxRow:", maxRow)

	f = excelize.NewFile()

	// Create a new sheet.
	index := f.NewSheet(sheet)
	// Set value of a cell.

	rowH := os.Getenv("RowH")
	lineH := 30.0

	if rowH != "" {
		lineH, _ = strconv.ParseFloat(rowH, 64)
	} else {
		rowH = "30"
	}
	//新excel的行
	cur_row := 1

	rfs = make([]ExcelFile, maxChan, maxChan+1)

	//ch := make(chan bool, maxChan)
	startTime := time.Now().Unix()
	fmt.Println("startTime:", startTime)
	//fmt.Println(newList)
	reg := regexp.MustCompile(`[^\x00-\xff]`)
	for _, vv := range rows {

		f.SetRowHeight(sheet, cur_row, lineH)
		if vv == "" {
			maxRow = cur_row
			continue
		}
		nn := reg.ReplaceAllString(vv, "")
		f.SetCellValue(sheet, subClass+strconv.Itoa(cur_row), html.UnescapeString(nn))
		cur_row += 1
	}

	time.Sleep(2 * time.Second)
	wg.Wait()

	fmt.Println("endTime:", time.Now().Unix())
	fmt.Println("sumTime:", time.Now().Unix()-startTime)
	// Set active sheet of the workbook.
	f.SetActiveSheet(index)
	// Save spreadsheet by the given path.

	pathnew := ROOT_PATH + "/export" + util.GetDateSp() + ".xlsx"

	if err := f.SaveAs(pathnew); err != nil {
		fmt.Println(err)
	}

	//fmt.Println(nk)

}
